(function () {
    var qnaContent = {
        "0": {
            "question": "保障以下哪些内容?",
            "answer": ["A、出行意外（海陆空出行全覆盖）", "B、日常意外（火灾，摔伤，烫伤等）", "C、出行意外+日常意外全覆盖"],
            "correctOption": "2"
        },
        "1": {
            "question": "中的“一人投保，全家受益”是指?",
            "answer": ["A、购买家庭版的，年满6个月-65周岁的配偶和子女都在保障范围内", "B、购买家庭版的，投保人可以指定家庭成员作为受益人"],
            "correctOption": "0"
        },
        "2": {
            "question": "购买家庭版比个人版更贵么?",
            "answer": ["A、同等价格，都是360元/年", "B、家庭版的价格是个人版的2倍"],
            "correctOption": "0"
        },
        "3": {
            "question": "的保障金额可以累加么?",
            "answer": ["A、可以，每份保额100万，最多购买2份，保额可累加至200万", "B、不可以，每份保单的保费为100万"],
            "correctOption": "0"
        },
        "4": {
            "question": "相对其他意外险而言，优势在哪?",
            "answer": ["A、出行意外+日常意外全覆盖", "B、每天＜1元，百万保障", "C、一人投保,全家受益", "D、以上均是"],
            "correctOption": "3"
        }
    }

    var qnaNumber = 0;
    var setChoice = false;
    var userAnswer = [false, false, false, false, false];
    var couponsNum = [20, 30, 50, 0];

    //设置问题内容、答案内容以及按钮样式
    var setQna = function () {
        $(".qna-answers-list").empty();
        $(".qna-number").text(qnaNumber + 1);
        $(".qna-question-main").text(qnaContent[qnaNumber].question);
        qnaContent[qnaNumber].answer.forEach(function (element, index) {
            var li = $("<li></li>").text(element);
            li.click(function () {
                li.siblings().removeClass("qna-answer-choice").removeClass("qna-right-choice").removeClass("qna-wrong-choice");
                li.addClass("qna-answer-choice");
                var correctOption = qnaContent[qnaNumber].correctOption;
                if (index == correctOption) {
                    li.addClass("qna-right-choice");
                    userAnswer[qnaNumber] = true;
                } else {
                    li.addClass("qna-wrong-choice");
                    userAnswer[qnaNumber] = false;
                }
                if (qnaNumber < 4) {
                    $(".next-btn").removeClass("next-btn-disabled").addClass("next-btn-abled");
                } else {
                    $(".next-btn").removeClass("next-btn-disabled").removeClass("next-btn-abled").removeClass("submit-btn-disabled").addClass("submit-btn-abled");
                }
                setChoice = true;
            });
            $(".qna-answers-list").append(li);
        }, this);
    }

    //设置下一个问题的index以及按钮样式
    var nextQna = function () {
        window._hmt && window._hmt.push(['_trackEvent', '意外险活动', '下一题或提交答案', qnaNumber]);
        qnaNumber++;
        var qnaProgress = document.getElementsByClassName("qna-progress")[qnaNumber];
        qnaProgress.className = "qna-progress qna-progress-end";
        if (qnaNumber < 4) {
            $(".next-btn").addClass("next-btn-disabled").removeClass("next-btn-abled");
        } else {
            $(".next-btn").removeClass("next-btn-disabled").removeClass("next-btn-abled").addClass("submit-btn-disabled");
        }
        setChoice = false;
    }

    var isFinished = function() {
        var userId = $.url().param("userId");
        var userType = $.url().param("userType");
        if (userId == "") {
            userId = sessionStorage.getItem("userId");
            userType = sessionStorage.getItem("userType");
        }
        sessionStorage.setItem("userId", userId);
        sessionStorage.setItem("userType", userType);
        if(userId == "" || userId == undefined) {
            var logginUrl = "https://auth.cx580.com/Auth.aspx?authType=app&userType=app&clientId=CheWu&redirect_uri=";
            window.location.href = logginUrl + window.location.href;
        }else{
            $.ajax({
                url: "../../Handler/UserCouponHandler.ashx?Action=getCoupon",
                type: "POST",
                data: {
                    userId: userId,
                    userType: userType
                    //errorCount: 0
                },
                success: function (res) {
                    var jsonData = JSON.parse(res);
                    var isGet =localStorage.getItem("isGet");
                    if (jsonData.status) {
                        var couponId = jsonData.data;
                        if(couponId.length!=0) {
                            if (couponId == "100221") {
                                window.location.replace("qna-fail.html?coupon=" + jsonData.data);
                            }
                            else {
                                window.location.replace("qna-success.html?coupon=" + jsonData.data);
                            }
                        }
                    }
                }
            });
        }
    }

    var init = function () {
        var isSubmit = false;
        var theCouponId = "";
        //刮奖浮层
        var success = document.getElementById("success");
        var successFl = new FloatLayer(success);

        //礼包浮层
        var giftPackage = document.getElementById("gift-package");
        var giftPackageFl = new FloatLayer(giftPackage);

        //礼包浮层
        var giftPackageFail = document.getElementById("gift-package-fail");
        var giftPackageFailFl = new FloatLayer(giftPackageFail);

        //根据用户答题弹出浮层
        $(".next-btn").click(function () {
            if (setChoice && qnaNumber != 4) {
                nextQna();
                setQna();
            } else if (setChoice && qnaNumber == 4) {
                //判断用户是否全对
                var isSucceed = userAnswer.indexOf(false) == -1;
                if (isSucceed) {
                    if(!isSubmit) {
                        localStorage.setItem("isGet","0");
                        sendCoupon(0);
                    }
                    $(".success-floatLayer").css("top", "0");
                    //$("#card-content").text(couponsNum[3] + 2 + "元礼包");
                    //$(".gift-package-price").text(couponsNum[3] + 2);
                    //$(".insurance-coupons-btn").addClass("insurance-coupons-btn" + couponsNum[3]);
                    //successFl.show();
                    //$(".success-floatLayer").css("top", "0");
                } else {
                    sendCoupon(1);
                    giftPackageFailFl.show();
                }
            }
        });

        //监听关闭刮奖浮层的按钮
        $("#success-close-btn").click(function () {
            $(".success-floatLayer").css("top", "150%");
        });

		$(function () {
            $(".gotoPage").click(function () {
                window._hmt && window._hmt.push(['_trackEvent', '意外险活动', '挑战失败马上使用', '挑战失败马上使用']);
                var piccPage = "https://chewu.cx580.com/OrderSite/page/picc/PiccIndex.aspx?userType=app";
                window.location.replace(piccPage);
                // window.cx580.jsApi.call(
                // {
                //     "commandId": "",
                //     "command": "jump",
                //     "data": { "target": "couponList" }
                // });
            });
        });

        function sendCoupon(errorCount) {
            var userId = sessionStorage.getItem("userId");
            var userType = sessionStorage.getItem("userType");
            $.ajax({
                url: "../../Handler/UserCouponHandler.ashx?Action=sendCoupon",
                type: "POST",
                data: {
                    userId: userId,
                    userType: userType,
                    errorCount: errorCount
                },
                success: function (res) {
                    var jsonData = JSON.parse(res);
                    console.log(jsonData);
                    if (jsonData.status && jsonData.data.length != 0) {
                        var couponId = jsonData.data;
                        setCouponPrice(couponId);
                    } else {
                        if(jsonData.data.length!=0){
                            setCouponPrice(jsonData.data);
                        }
                        var sendFail = document.getElementById("send-fail");
                        var sendFailFl = new FloatLayer(sendFail);
                        $(".send-fail-tip").text(jsonData.message);
                        var isGet =localStorage.getItem("isGet");
                        if(isGet==undefined || isGet=="1")
                            sendFailFl.show();
                        $("#send-fail").click(function () {
                            sendFailFl.hide();
                        });
                        //发送礼包失败 提示用户
                    }
                }
            });
        }

        function setCouponPrice(couponId) {
            theCouponId = couponId;
            if (couponId == "100221") {
                return;
            } else {
                if (couponId == "100218") {
                    couponsNum[3] = 50;
                } else if (couponId == "100219") {
                    couponsNum[3] = 30;
                } else {
                    couponsNum[3] = 20
                }
                $("#card-content").text(couponsNum[3] + 2 + "元礼包");
                $(".gift-package-price").text(couponsNum[3] + 2);
                $(".insurance-coupons-btn").addClass("insurance-coupons-btn" + couponsNum[3]);
                isSubmit = true;
                //成功
            }
        }
        //监听关闭礼包浮层的按钮
        $("#package-close-btn").click(function () {
            var coupon = $.url().param("coupon");
            if(coupon == "" || coupon == undefined){
                coupon = theCouponId
            }
            window.location.replace("./qna-success.html?coupon=" + coupon);
        });
        $("#fail-package-close-btn").click(function () {
            var coupon = $.url().param("coupon");
            if(coupon == "" || coupon == undefined){
                coupon = theCouponId
            }
            window.location.replace("./qna-fail.html?coupon=" + coupon);
        });
        $(".btn_success").click(function(){
            //var coupon = $.url().param("coupon");
            //if(coupon == "" || coupon == undefined){
           //     coupon = theCouponId
            //}
            //window.location.replace("./qna-success.html?coupon=" + coupon);
			window.cx580.jsApi.call(
                {
                    "commandId": "",
                    "command": "jump",
                    "data": { "target": "couponList" }
                });
        });
        $(".insurance-coupons-btn").click(function(){
            window._hmt && window._hmt.push(['_trackEvent', '意外险活动', '挑战成功意外险券', '挑战成功意外险券']);
        });
        $(".refuel-coupons-btn").click(function(){
            window._hmt && window._hmt.push(['_trackEvent', '意外险活动', '挑战成功加油券', '挑战成功意加油券']);
        });

        //刮刮乐设置
        LuckyCard.case({
            coverImg: './images/scratch-off-btn.png',
            ratio: .7,
            callback: function () {
                successFl.hide();
                giftPackageFl.show();
				localStorage.setItem("isGet","1");
                window._hmt && window._hmt.push(['_trackEvent', '意外险活动', '刮奖', "刮奖"]);
            }
        });
    }
    window.onload = function () {
        isFinished();
        init();
        setQna();
    }
})();